package eu.easyedu.dictionary.gui;

import eu.easyedu.dictionary.api.nservice.spi.DictionaryService;
import eu.easyedu.dictionary.api.options.DictionaryOptions;
import eu.easyedu.dictionary.api.util.ConnectionDoesNotExistsException;
import eu.easyedu.dictionary.api.util.DatabaseUtils;
import eu.easyedu.dictionary.gui.resources.Bundle;
import java.awt.event.ActionEvent;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;
import org.openide.util.ImageUtilities;
import org.openide.util.Lookup;
import org.openide.windows.TopComponent;

/**
 * Action which shows MainDictionary component.
 */
public class MainDictionaryAction extends AbstractAction {

    private static final long serialVersionUID = 1L;
    private static final Logger log = Logger.getLogger(MainDictionaryAction.class.getName());
    private static final String NO_CONN_MSG_PROPERTY = "MainDictionaryTopComponent.noConnection.message";
    private static final String NO_CONN_TITLE_PROPERTY = "MainDictionaryTopComponent.noConnection.title";
    private static final String NO_DICT_MSG_PROPERTY = "MainDictionaryTopComponent.noDictionary.message";
    private static final String NO_DICT_TITLE_PROPERTY = "MainDictionaryTopComponent.noDictionary.title";

    public MainDictionaryAction() {
        super(Bundle.getMessage("CTL_MainDictionaryAction"));
        putValue(SMALL_ICON, new ImageIcon(ImageUtilities.loadImage(MainDictionaryTopComponent.ICON_PATH, true)));
    }

    public void actionPerformed(ActionEvent evt) {
        boolean canShow = false;
        if (DatabaseUtils.containsSupportedConnection()) {
            canShow = true;
        } else {
            NotifyDescriptor desc = new NotifyDescriptor.Confirmation(
                    Bundle.getMessage(NO_CONN_MSG_PROPERTY),
                    Bundle.getMessage(NO_CONN_TITLE_PROPERTY),
                    NotifyDescriptor.YES_NO_OPTION, NotifyDescriptor.WARNING_MESSAGE);
            if (DialogDisplayer.getDefault().notify(desc) == NotifyDescriptor.YES_OPTION) {
                String connName = null; //DatabaseUtils.createDefaultConnection();
                if (connName != null) {
                    DictionaryOptions.getInstance().setConnectionName(connName);
                    boolean containsDictionary = false;
                    try {
                        // FIXME: dictionary shouldn't be null
                        containsDictionary = DatabaseUtils.checkIfConnectionContainsDictionary(null);
                    } catch (ConnectionDoesNotExistsException e) {
                        log.severe(e.getMessage());
                    }
                    if (!containsDictionary) {
                        desc = new NotifyDescriptor.Confirmation(
                                Bundle.getMessage(NO_DICT_MSG_PROPERTY),
                                Bundle.getMessage(NO_DICT_TITLE_PROPERTY),
                                NotifyDescriptor.YES_NO_OPTION, NotifyDescriptor.WARNING_MESSAGE);
                        if (DialogDisplayer.getDefault().notify(desc) == NotifyDescriptor.YES_OPTION) {
                            DictionaryService dictionaryService = Lookup.getDefault().lookup(DictionaryService.class);
                            // FIXME: cannot be null!
                            dictionaryService.createDictionaryDDL(null);
                            canShow = true;
                        }
                    } else {
                        canShow = true;
                    }
                }
            }
        }
        if (canShow) {
            // FIXME: Question is: Do we need this action if we have OpenDictionaryAction?
            log.warning("CO TO ROBIM??");
            TopComponent win = MainDictionaryTopComponent.findInstance(null);
            win.open();
            win.requestActive();
        }
    }
}
